﻿Imports System.Xml
Public Class frmProductCategoryManage

  Private m_NameWidth As Integer = 200
  Private m_ManufacturerWidth As Integer = 200
  Private m_PriceWidth As Integer = 80
  Private m_UnitWidth As Integer = 80

  Private m_CurrentProductCateName As String = ""
  Private m_CurrentProductCateID As Integer = 0
  Private m_CurProductCateManua As String = ""
  Private m_CurProductCatePrice As String = ""
  Private m_CurProductCateUnit As String = ""
  Private m_CurProductCateCom As String = ""

  Enum eControl
    ADDED = 0
    EDITED
  End Enum

  Private m_SelectedControl As eControl


  Private Sub frmProductCategoryManage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    cmbUnit.SelectedIndex = 0
    'Update data on grid
    LoadDataOnGrid()

    'Update controls
    UpdateContronls()

    'Clear control input
    ClearControlInput()

    If dgvProductCategoryList.RowCount > 0 Then
      'Get current product category name
      SetDefaultProductCate()

      Dim FuntionandSubObj As FuntionandSub = New FuntionandSub()
      FuntionandSubObj.HighlightRow(Me.dgvProductCategoryList, m_CurrentProductCateID)
    End If

    'Disable controls
    DisableCtrols()
  End Sub

  Private Sub HeadofGrid()
    With dgvProductCategoryList
      'Column 0
      .Columns(0).Name = "ID"
      .Columns(0).Width = 0
      .Columns(0).Visible = False
      .Columns(0).ReadOnly = True

      'Column 1
      .Columns(1).HeaderText = "Loại Sản Phẩm"
      .Columns(1).Name = "Name"
      .Columns(1).Width = m_NameWidth
      .Columns(1).ReadOnly = True
      .Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

      'Column 2
      .Columns(2).HeaderText = "Nhà Sản Xuất"
      .Columns(2).Name = "Manufacture"
      .Columns(2).Width = m_ManufacturerWidth
      .Columns(2).ReadOnly = True
      .Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

      'Column 3
      .Columns(3).HeaderText = "Giá"
      .Columns(3).Name = "Price"
      .Columns(3).Width = m_PriceWidth
      .Columns(3).ReadOnly = True

      'Column 4
      .Columns(4).HeaderText = "Đơn Vị"
      .Columns(4).Name = "Unit"
      .Columns(4).Width = m_UnitWidth
      .Columns(4).ReadOnly = True

      'Column 5
      .Columns(5).HeaderText = "Ghi Chú"
      .Columns(5).Name = "Comments"
      .Columns(5).Width = dgvProductCategoryList.Width - (m_NameWidth + m_ManufacturerWidth + m_PriceWidth + m_UnitWidth)
      .Columns(5).ReadOnly = True

    End With
  End Sub

  Private Function AddProCate() As Boolean
    Dim productName As String = txtName.Text.Trim()

    If productName.Equals("") Then
      MessageBox.Show("Bạn phải nhập tên cho loại sản phẩm.", "Loại Sản Phẩm", MessageBoxButtons.OK, MessageBoxIcon.Error)
      txtName.Focus()
      Return False
    End If

    Dim dbAccess As DataBaseAccess = New DataBaseAccess
    Dim SQLString As String = ""
    Dim dtTable As DataTable
    Dim dtRow As DataRow
    Dim Price As String = txtPrice.Text.Trim()
    Convert.ToDouble(IIf(String.IsNullOrEmpty(Price), 0, Price))

    SQLString = "SELECT * FROM tbl_restorationcategories WHERE Name = '" & productName & "' AND Price = '" & Price & "'"
    dtTable = dbAccess.GetDataTable(SQLString)
    If dtTable.Rows.Count() > 0 Then
      MessageBox.Show("Loai sản phẩm này đã tồn tại.", "Loại Sản Phẩm", MessageBoxButtons.OK, MessageBoxIcon.Error)
      txtName.Focus()
      Return False
    End If
    Try
      SQLString = "SELECT * FROM tbl_restorationcategories"
      dtTable = dbAccess.GetDataTable(SQLString)
      dtRow = dtTable.NewRow
      SetProductCateData(dtRow)
      dtTable.Rows.Add(dtRow)
      dbAccess.UpdateDataTable(SQLString, dtTable)
      'Update data on grid
      LoadDataOnGrid()

      'Update controls
      UpdateContronls()

      'Clear control input
      ClearControlInput()

      SQLString = "SELECT ID FROM tbl_restorationcategories"
      dtTable = dbAccess.GetDataTable(SQLString)
      Dim FuntionandSubObj As FuntionandSub = New FuntionandSub()
      m_CurrentProductCateID = FuntionandSubObj.GetID_Max(dtTable)
      If m_CurrentProductCateID <> 0 Then
        FuntionandSubObj.HighlightRow(Me.dgvProductCategoryList, m_CurrentProductCateID)
      End If
    Catch ex As Exception
      Throw New Exception(" Can not insert material record ")
      Return False
    End Try

    Return True
  End Function

  Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    m_SelectedControl = eControl.ADDED
    EnableCtrols()
    ClearControlInput()
  End Sub

  Private Sub SetProductCateData(ByRef drow As DataRow)
    drow("Name") = txtName.Text.Trim()
    drow("Manufacture") = txtManufacture.Text.Trim()
    drow("Price") = Convert.ToDouble(IIf(String.IsNullOrEmpty(txtPrice.Text.Trim()), 0, txtPrice.Text.Trim()))
    drow("Unit") = cmbUnit.Text.Trim()
    drow("Comments") = txtComments.Text.Trim()
  End Sub

  Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
    'Enable controls
    EnableCtrols()

    EditProductCate()
  End Sub

  Public Function EditProductCate() As Boolean
    Try
      Dim dbAccess As DataBaseAccess = New DataBaseAccess()
      Dim SQLString = "SELECT * FROM tbl_restorationcategories WHERE ID = '" & m_CurrentProductCateID & "'"
      Dim dtTable = dbAccess.GetDataTable(SQLString)

      If dtTable.Rows.Count = 0 Then
        Return False
      End If
      Dim dtRow As DataRow = dtTable.Rows(0)

      dtRow.BeginEdit()
      SetProductCateData(dtRow)
      dtRow.EndEdit()
      dbAccess.UpdateDataTable(SQLString, dtTable)
      Return True
    Catch ex As Exception
      MessageBox.Show(" Bạn không thể sửa thông tin loại sản phẩm này " + ex.Message.ToString(), "Lỗi sửa dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Error)
      Return False
    End Try
  End Function

  Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    If (m_CurrentProductCateID <> 0) Then
      If (MsgBoxResult.Yes = MessageBox.Show("Bạn có muấn xóa loại sản phẩm này không?", "Loại Sản Phẩm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) Then
        If DeleteProCate(m_CurrentProductCateID) = True Then
          LoadDataOnGrid()

          If dgvProductCategoryList.RowCount > 0 Then
            SetDefaultProductCate()
            If m_CurrentProductCateID > 0 Then
              Dim FuntionandSubObj As FuntionandSub = New FuntionandSub()
              FuntionandSubObj.HighlightRow(Me.dgvProductCategoryList, m_CurrentProductCateID)

              DisplayValueOnCtrol()
            End If
          End If
        End If
      End If
    End If
  End Sub

  Public Function DeleteProCate(ByVal ProductCateID As Integer) As Boolean
    Try
      Dim dbAccess As DataBaseAccess = New DataBaseAccess
      Dim SQLString = "DELETE FROM tbl_restorationcategories WHERE ID = '" & ProductCateID & "'"
      dbAccess.ExecuteNoneQuery(SQLString)
    Catch ex As Exception
      MessageBox.Show(" Bạn không thể xóa loại sản phẩm này " + ex.Message.ToString(), "Loại Sản Phẩm", MessageBoxButtons.OK, MessageBoxIcon.Error)
      Return False
    End Try
    Return True
  End Function

  Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
    Me.Close()
    Me.DialogResult = Windows.Forms.DialogResult.OK
  End Sub

  Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

  End Sub

  Private Sub UpdateContronls()
    If dgvProductCategoryList.RowCount > 0 Then
      btnDelete.Enabled = True
      btnEdit.Enabled = True
      btnSelect.Enabled = True
      btnPrint.Enabled = True
    Else
      btnDelete.Enabled = False
      btnEdit.Enabled = False
      btnSelect.Enabled = False
      btnPrint.Enabled = False
    End If
  End Sub

  Private Sub LoadDataOnGrid()
    dgvProductCategoryList.DataSource = Nothing
    Dim SQLString As String = "SELECT ID, Name, Manufacture, Price, Unit, Comments FROM tbl_restorationcategories"
    Dim dtAccess As DataBaseAccess = New DataBaseAccess()
    Dim dtTable As DataTable = dtAccess.GetDataTable(SQLString)
    dgvProductCategoryList.DataSource = dtTable
    ' Set header for the grid
    HeadofGrid()
  End Sub

  Private Sub ClearControlInput()
    txtComments.Text = ""
    txtManufacture.Text = ""
    txtName.Text = ""
    txtPrice.Text = ""
  End Sub

  Private Sub DisplayValueOnCtrol()
    txtComments.Text = m_CurProductCateCom
    txtManufacture.Text = m_CurProductCateManua
    txtName.Text = m_CurrentProductCateName
    txtPrice.Text = m_CurProductCatePrice
    cmbUnit.Text = m_CurProductCateUnit
  End Sub

  Private Sub DisableCtrols()
    txtComments.Enabled = False
    txtManufacture.Enabled = False
    txtName.Enabled = False
    txtPrice.Enabled = False
    cmbUnit.Enabled = False
  End Sub

  Private Sub EnableCtrols()
    txtComments.Enabled = True
    txtManufacture.Enabled = True
    txtName.Enabled = True
    txtPrice.Enabled = True
    cmbUnit.Enabled = True
  End Sub

  Private Sub SetDefaultProductCate()
    If dgvProductCategoryList.RowCount > 0 Then
      Dim dtTable As DataTable = dgvProductCategoryList.DataSource
      Dim dtRow As DataRow = dtTable.Rows(0)
      m_CurrentProductCateID = IIf(IsDBNull(dtRow("ID")), 0, dtRow("ID"))
      m_CurrentProductCateName = IIf(IsDBNull(dtRow("Name")), "", dtRow("Name"))
      m_CurProductCateManua = IIf(IsDBNull(dtRow("Manufacture")), "", dtRow("Manufacture"))
      m_CurProductCatePrice = IIf(IsDBNull(dtRow("Price")), 0, dtRow("Price"))
      m_CurProductCateUnit = IIf(IsDBNull(dtRow("Unit")), "", dtRow("Unit"))
      m_CurProductCateCom = IIf(IsDBNull(dtRow("Comments")), "", dtRow("Comments"))
    End If
  End Sub

  Private Sub dgvProductCategoryList_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvProductCategoryList.CellClick
    If dgvProductCategoryList.Rows.Count > 0 Then
      m_CurrentProductCateID = IIf(IsDBNull(dgvProductCategoryList.CurrentRow.Cells(0).Value), 0, dgvProductCategoryList.CurrentRow.Cells(0).Value)
      m_CurrentProductCateName = IIf(IsDBNull(dgvProductCategoryList.CurrentRow.Cells(1).Value), "", dgvProductCategoryList.CurrentRow.Cells(1).Value)
      m_CurProductCateManua = IIf(IsDBNull(dgvProductCategoryList.CurrentRow.Cells(2).Value), "", dgvProductCategoryList.CurrentRow.Cells(2).Value)
      m_CurProductCatePrice = IIf(IsDBNull(dgvProductCategoryList.CurrentRow.Cells(3).Value), 0, dgvProductCategoryList.CurrentRow.Cells(3).Value)
      m_CurProductCateUnit = IIf(IsDBNull(dgvProductCategoryList.CurrentRow.Cells(4).Value), "", dgvProductCategoryList.CurrentRow.Cells(4).Value)
      m_CurProductCateCom = IIf(IsDBNull(dgvProductCategoryList.CurrentRow.Cells(5).Value), "", dgvProductCategoryList.CurrentRow.Cells(5).Value)

      DisplayValueOnCtrol()

      'Disable controls
      DisableCtrols()
    End If
  End Sub

  Public Function GetCurrentProductCateName() As String
    Return m_CurrentProductCateName
  End Function

  Public Function GetCurentProductCateID() As Integer
    Return m_CurrentProductCateID
  End Function

  Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Select Case m_SelectedControl
      Case eControl.ADDED
        AddProCate()
      Case eControl.EDITED
        
    End Select
  End Sub
End Class